package com.xzc.java8.A_lambda.lambda0;

import java.util.Comparator;

public class ComparatorTest {

    public static String getWeightApple(Apple a1, Apple a2, Comparator<Apple> comparator) {
        return comparator.compare(a1, a2) > 0 ? a1.getName() : a2.getName();
    }

    public static void main(String[] args) {
        Comparator<Apple> byWeightNormal = new Comparator<Apple>() {
            @Override
            public int compare(Apple a1, Apple a2) {
                return a1.getWeight() - a2.getWeight();
            }
        };

        Comparator<Apple> byWeightLambda = (Apple a1, Apple a2) -> a1.getWeight() - a2.getWeight();

        Apple a1 = new Apple();
        a1.setName("bigger");
        a1.setWeight(50);

        Apple a2 = new Apple();
        a2.setName("smaller");
        a2.setWeight(10);

        System.out.println(getWeightApple(a1, a2, byWeightNormal));
        System.out.println(getWeightApple(a1, a2, byWeightLambda));
        System.out.println(getWeightApple(a1, a2, (Apple a, Apple b) -> a.getWeight() - b.getWeight()));
    }

}
